package nl.grol.yakshop.domain.model;

import java.util.Date;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

import com.google.common.base.Objects;
import com.google.common.base.Preconditions;

@PersistenceCapable
public class ProcessedOrder extends BaseOrder {
	
    @Persistent
	private Date processedDate;

    @Persistent
	private ProcessedOrderStatus orderStatus;
	
	public ProcessedOrder( Integer forDay, Date receiptDate,
							String customerName, Double milkInLiters, 
							Integer skins ) {
		super( forDay, customerName, milkInLiters, skins, receiptDate);
		this.processedDate = new Date();
		
		Preconditions.checkArgument(this.getForDay() != null);
		Preconditions.checkArgument(this.getForDay().intValue() >= 0 );
		Preconditions.checkArgument(this.getReceiptDate() != null);
		Preconditions.checkArgument(this.getCustomerName() != null);
		Preconditions.checkArgument(this.processedDate != null);

		/*
		 *  The parameters milkInLiters and skins can be null.
		 *  If null it is an indicator to note that the order was not fulfiiled
		 */
		if( this.getMilkInLiters() == null && this.getSkins() == null ) {
			this.orderStatus = ProcessedOrderStatus.UNFULLFILLED;
		} else if( this.getMilkInLiters() != null && this.getSkins() != null ) {
			this.orderStatus = ProcessedOrderStatus.COMPLETELY_FULLFILLED;
		} else {
			this.orderStatus = ProcessedOrderStatus.PARTIALLY_FULFILLED;
		}
	}

	public Date getProcessedDate() {
		return processedDate;
	}

	public ProcessedOrderStatus getOrderStatus() {
		return orderStatus;
	}
	
	public String toString() {
		
		return Objects.toStringHelper(this)
	       .add("key", this.getKey())
	       .add("forDay", this.getForDay())
	       .add("receiptDate", this.getReceiptDate())
	       .add("customerName", this.getCustomerName())
	       .add("milkInLiters", this.getMilkInLiters())
	       .add("skins", this.getSkins())
	       .add("processedDate", this.processedDate)
	       .add("orderStatus", this.orderStatus.toString())
	       .toString();
	}

	
}
